;(function(){
  'use strict';
  angular.module('controllers.faultLocation.failureLocation')
    .controller('failureNoticeCtrl',failureNoticeCtrl);

  function failureNoticeCtrl($scope,COREDATA,CoreServer,$cookies,LoginServer,CoreService,FailureNoticeServer,$mdDialog,CoreLoading){
    var queryedSystem = '';
    $scope.selector = {
      systemCode:COREDATA.SYSTEM,
      provinceCode:COREDATA.PROVINCES,
      sendTypes:[{sendTypeName:'邮件',sendTypeCode:'1'},{sendTypeName:'短信',sendTypeCode:'2'}],
      frequencyTypes:[{frequencyTypeName:'小时',frequencyTypeCode:'2'},{frequencyTypeName:'分钟',frequencyTypeCode:'1'},{frequencyTypeName:'秒',frequencyTypeCode:'0'}]
    };
    $scope.failureNotice = {
      frequencyInterval : 30,
      frequencyType : $scope.selector.frequencyTypes[1]
    };

    $scope.getBusinesses = function(systemCode){
      if(!systemCode){
        $scope.failureNotice.systemCode = $scope.selector.systemCode[0];
        systemCode = $scope.failureNotice.systemCode;
      }
      if(queryedSystem === systemCode){
        return;
      }
      return CoreServer.getSysBusiness({systemCode:systemCode.systemCode},function(res){
        if(res.state === '10000'){
          $scope.selector.businessCode = res.data;
          queryedSystem = systemCode;
        }
      });
    };
    initNotice();

    $scope.modifyNotice = function($event){
      if(!$cookies.getObject('userCookie')){
        CoreService.dialog('用户信息验证失效,请重新登录');
        LoginServer.signOut();
      }
      var confirm = $mdDialog.confirm()
        .title('提示')
        .content('是否保存当前通知配置')
        .cancel('取消')
        .ok('确认')
        .targetEvent($event);
      $mdDialog.show(confirm).then(sureToModify);
      function sureToModify(){
        var param = {};
        param.provinceCode = $scope.failureNotice.provinceCode ? $scope.failureNotice.provinceCode.provinceCode : '200';
        param.systemCode = $scope.failureNotice.systemCode ? $scope.failureNotice.systemCode.systemCode : $scope.selector.systemCode[0].systemCode;
        param.businessCode =  $scope.failureNotice.businessCode.businessCode;
        param.sendType = '1';
        param.frequencyType = $scope.failureNotice.frequencyType ? $scope.failureNotice.frequencyType.frequencyTypeCode : '1';
        param.frequencyInterval = is.not.nan($scope.failureNotice.frequencyInterval) ? Number($scope.failureNotice.frequencyInterval) : 1;
        param.creater = $cookies.getObject('userCookie').userId;
        CoreLoading.setState(true);
        FailureNoticeServer.updateNotice(param,function(res){
          if(res.state === '10000'){
            CoreLoading.setState(false);
            modifyNotice(param);
            CoreService.dialog('保存成功');
          }else{
            CoreLoading.setState(false);
            CoreService.dialog('10003');
          }
        });
      }
    };

    function modifyNotice(notice){
      $cookies.putObject('modelNotice',notice);
    }

    function initNotice(){
      var notice = $cookies.getObject('modelNotice');
      if(!notice){
        return;
      }
      if(notice.provinceCode){
        angular.forEach($scope.selector.provinceCode,function(item){
          if(item.provinceCode === notice.provinceCode){
            $scope.failureNotice.provinceCode = item;
          }
        });
      }
      if(notice.sendType){
        angular.forEach($scope.selector.sendType,function(item){
          if(item.sendTypeCode === notice.sendType){
            $scope.failureNotice.sendType = item;
          }
        });
      }

      if(notice.systemCode){
        angular.forEach($scope.selector.systemCode,function(item){
          if(item.systemCode === notice.systemCode){
            $scope.failureNotice.systemCode = item;
            $scope.getBusinesses(item.systemCode).then(function(){
              if(notice.businessCode){
                angular.forEach($scope.selector.businessCode,function(item){
                  if(item.businessCode === notice.businessCode){
                    $scope.failureNotice.businessCode = item;
                  }
                });
              }
            });
          }
        });
      }

      if(notice.frequencyType){
        angular.forEach($scope.selector.frequencyTypes,function(item){
          if(item.frequencyTypeCode === notice.frequencyType){
            $scope.failureNotice.frequencyType = item;
          }
        });
      }
      $scope.failureNotice.frequencyInterval = is.not.nan(notice.frequencyInterval)? Number(notice.frequencyInterval):1;
    }
    
  }
})();